package me.helllp.bootman.common.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class PasswordHelper {
	private static RandomNumberGenerator randomNumberGenerator =  new SecureRandomNumberGenerator();  
	
	private static final String algorithmName = "md5";  

	private static final int hashIterations = 2;
	
	/**
	 * 生成盐
	 * 
	 * @return
	 */
	public static String generatorSalt(){
		return randomNumberGenerator.nextBytes().toHex();
	}
	
	/**
	 * 生成密码+盐
	 */
    public static String encryptPassword(String password, String salt) {
        return new SimpleHash(  
                algorithmName,  
                password,  
                ByteSource.Util.bytes(password + salt),  
                hashIterations).toHex();
    }  

    /**
     * 进行密码的比较
     * 
     * @return
     */
    public static boolean veryfyPassword(String password, String salt, String encPassword){
    	return encryptPassword(password,salt).equals(encPassword);
    }
}
